package com.deyang.util;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.deyang.widget.Divider;


/**
 * @author 王德阳
 * @createDate 2018/6/19 14:28
 * @desc
 */
public class ViewUtil {
    /**
     * 给TextView 指定下标位置 设置字体颜色
     *
     * @param textView
     * @param text     要设置的内容
     * @param start    要设置的起位置
     * @param ent      要设置的终位置
     * @param color    要设置的颜色
     */
    public static void setViewTextPartTextColor(TextView textView, String text, int start, int ent, int color) {
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(text);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
        stringBuilder.setSpan(colorSpan, start, ent, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(stringBuilder);
    }

    /**
     * 修改文字中字体的颜色
     *
     * @param originalStrs 原文字
     * @param updateTexts  要修改的文字
     * @param updateColor  要修改的颜色
     * @return
     */
    @NonNull
    public static SpannableStringBuilder updateTextColor(String originalStrs, String[] updateTexts, int updateColor) {
        SpannableStringBuilder style = new SpannableStringBuilder(originalStrs);
        for (int i = 0; i < updateTexts.length; i++) {
            int updateColorText = originalStrs.indexOf(updateTexts[i]);
            int updatefend = updateColorText + updateTexts[i].length();
            style.setSpan(new ForegroundColorSpan(updateColor), updateColorText, updatefend, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        }
        return style;
    }

    /**
     * 设置控件 编辑状态
     *
     * @param editText
     * @param isEdit   设置是否可编辑状态
     */
    public static void setEdit(Activity activity, EditText editText, boolean isEdit) {
        // 可编辑
        if (isEdit) {
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            editText.requestFocus();
            editText.findFocus();
            activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            editText.setSelection(editText.getText().toString().length());


        } else { //不可编辑
            editText.setFocusable(false);
            editText.setFocusableInTouchMode(false);
        }
    }

    /**
     * 设置控件 编辑状态
     *
     * @param editTexts
     * @param isEdit    设置是否可编辑状态
     */
    public static void setEdits(Activity activity, EditText[] editTexts, boolean isEdit) {
        // 可编辑
        if (isEdit) {
            for (EditText editText : editTexts) {
                editText.setFocusable(true);
                editText.setFocusableInTouchMode(true);
                editText.requestFocus();
                editText.findFocus();
                activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                editText.setSelection(editText.getText().toString().length());
            }

        } else { //不可编辑
            for (EditText editText : editTexts) {
                editText.setFocusable(false);
                editText.setFocusableInTouchMode(false);
            }
        }
    }

    /**
     * recycleVIew  输入框错位
     *
     * @param et_name
     * @param nameWatcher
     */
    public static void setFocusChangeListener(final EditText et_name, final TextWatcher nameWatcher) {
        et_name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    et_name.addTextChangedListener(nameWatcher);
                } else {
                    et_name.removeTextChangedListener(nameWatcher);
                }
            }
        });
    }

    /**
     * 设置 控件 高度 = 宽度
     *
     * @param view
     */
    public static void setHeightForWidth(final View view) {
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                layoutParams.height = view.getWidth();
                view.setLayoutParams(layoutParams);
            }
        });
    }

    /**
     * 设置 控件 高度 = 宽度  比例
     *
     * @param view
     * @param height_widthProportion 高:宽
     */
    public static void setHeight_Width(final View view, final float height_widthProportion) {
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                layoutParams.height = (int) (view.getWidth() * height_widthProportion);
                view.setLayoutParams(layoutParams);
            }
        });
    }


    /**
     * 初始化 recycleVIew
     *
     * @param context
     * @param isAddDivider
     * @param recyclerView
     * @param adapter
     */
    public static void initRecycleView(Context context, boolean isAddDivider, RecyclerView recyclerView, RecyclerView.Adapter adapter) {
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setFocusable(false);
        if (isAddDivider) {
            recyclerView.addItemDecoration(new Divider(context));
        }
        recyclerView.setAdapter(adapter);
    }
}
